package com.lnsf.bookstore.system.comment.controller;

import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.common.annotation.IgnoreAuth;
import com.lnsf.bookstore.common.controller.ApiBaseAction;
import com.lnsf.bookstore.common.dto.ResultSuccessDto;
import com.lnsf.bookstore.system.comment.service.CommentService;
import com.lnsf.bookstore.system.comment.vo.Comment;
import com.lnsf.bookstore.web.book.service.ApiBookService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping(value = "/comment")
public class CommentController extends ApiBaseAction{
	
	@Autowired
	private CommentService commentService;
	@Autowired
	private ApiBookService apiBookService;

	@RequestMapping("/v_list.do")
	public String list(HttpServletRequest request, ModelMap modelMap){
		System.out.println("comment界面.....");
		return "admin/comment/comment";
	}

	/**
	 * @param bookId
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Object list( Integer bookId,
					   @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size,
					   String sort, String order) {
		Map<String, Object> params = new HashMap();
		Map<String, Object> map = new HashMap<String, Object>();
		params.put("bookId", bookId);
		params.put("page", page);
		params.put("limit", size);
		PageInfo<Comment> commentList = commentService.getPage(params);
		Integer total = commentService.queryTotal(params);
		map.put("page",commentList);
		return map;
	}

	/**
	 * 查看信息
	 */
	@RequestMapping("/info/{id}")
	@ResponseBody
	//@RequiresPermissions("comment:info")
	public Object info(@PathVariable("id") Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		Comment comment = commentService.queryObject(id);
		map.put("comment", comment);
		return map;
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@ResponseBody
	//@RequiresPermissions("comment:save")
	public Object save(@RequestBody Comment comment) {
		commentService.save(comment);

		return new ResultSuccessDto("保存成功");
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@ResponseBody
	//@RequiresPermissions("comment:update")
	public Object update(@RequestBody Comment comment) {
		commentService.update(comment);

		return new ResultSuccessDto("修改成功");
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	//@RequiresPermissions("comment:delete")
	public Object delete(@RequestBody Integer[] ids) {
		commentService.deleteBatch(ids);

		return new ResultSuccessDto("删除成功");
	}

	/**
	 * 修改状态
	 */
	@RequestMapping("/toggleStatus")
	@ResponseBody
	//@RequiresPermissions("comment:toggleStatus")
	public Object toggleStatus(@RequestBody Comment comment) {
		commentService.toggleStatus(comment);

		return new ResultSuccessDto("操作成功");
	}
}
